#ifndef CVS_ATOMIC_H_
#define CVS_ATOMIC_H_
// d:\Program Files\DevelopTools\third_party\boost\boost_1_54_0\boost\smart_ptr\detail\atomic_count_pthreads.hpp
#include <stdint.h>
#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/detail/atomic_count.hpp>
#include <boost/thread/mutex.hpp>

namespace cvs {
	namespace detail {
		template<class T>
		class AtomicIntegerT : boost::noncopyable {
		public:
			AtomicIntegerT() : value_(0) {}
			~AtomicIntegerT() {}

			T get() const {
				boost::mutex::scoped_lock lock(mutex_);

				//return value_;
				return const_cast<const volatile T&>(value_);
			}

			T getAndAdd(T x) {
				boost::mutex::scoped_lock lock(mutex_);
				T tmp = value_;
				value_+=x;

				return tmp;
			}

			T addAndGet(T x) {
				return getAndAdd(x) + x;
			}

			T incrementAndGet() {
				return addAndGet(1);
			}

			void add(T x) {
				getAndAdd(x);
			}

			void increment() {
				incrementAndGet();
			}

			void decrement() {
				getAndAdd(-1);
			}

			T getAndSet(T newValue) {
				T tmp = value_;
				value_ = newValue;

				return tmp;
			}
		
		private:
			mutable boost::mutex mutex_;
			volatile T value_;
		};
	}

	typedef detail::AtomicIntegerT<int32_t> AtomicInt32;
	typedef detail::AtomicIntegerT<int64_t> AtomicInt64;
}

#endif